﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变
{
    public partial class carTypeManager : Form
    {
        public carTypeManager()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }
        public void LoadMsg()
        {
            string sql = "select * from CarTypeInfo";
            DataTable dt = DBHelper.ExecuteQuery(sql);
            //设置是否允许自动生成列
            carTypeList.AutoGenerateColumns = false;
            carTypeList.DataSource = dt;
        }

        private void carTypeManager_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            LoadMsg();
        }

        private void carTypeList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtId.Text = carTypeList.SelectedRows[0].Cells[0].Value.ToString();
            txtTitle.Text = carTypeList.SelectedRows[0].Cells[1].Value.ToString();
            btnSubmit.Text = "修改";
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlParameter[] ps =
           {
                 new SqlParameter("@CTypeId", txtId.Text),
                 new SqlParameter("@CTitle", txtTitle.Text)
        };


            if (txtId.Text == "添加时无编号")
            {
                //添加
                string sql = "insert into CarTypeInfo(CTitle,CIsDelete) values(@CTitle,0)";
                int fin = DBHelper.ExecuteNonQuery(sql, ps);
                if (fin > 0)
                {
                    this.ShowSuccessTip("添加成功");
                    LoadMsg();
                    //this.DialogResult = DialogResult.OK;
                }
                else
                {
                    this.ShowErrorTip("添加失败");
                    //this.DialogResult = DialogResult.Cancel;
                }
            }
            else
            {
                string sql = "update CarTypeInfo set CTitle=@CTitle,CIsDelete=0 where CTypeId = @CTypeId";
                int res = DBHelper.ExecuteNonQuery(sql, ps);
                if (res > 0)
                {
                    this.ShowSuccessTip("修改成功");
                    LoadMsg();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    this.ShowErrorTip("修改失败");
                    this.DialogResult = DialogResult.Cancel;

                }
            }
            clear();
        }

        public void clear()
        {
            txtTitle.Text = "";
            txtId.Text = "添加时无编号";
            btnSubmit.Text = "添加";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }

        //删除（批量）
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult RSS = MessageBox.Show(this, "确定要删除选中行数据码？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            switch (RSS)
            {
                case DialogResult.Yes:
                    for (int i = this.carTypeList.SelectedRows.Count; i > 0; i--)
                    {
                        int CTypeId = Convert.ToInt32(carTypeList.SelectedRows[i - 1].Cells[0].Value);
                        carTypeList.Rows.RemoveAt(carTypeList.SelectedRows[i - 1].Index);
                        //使用获得的ID删除数据库的数据
                        string sql = "delete  from CarTypeInfo where CTypeId=@CTypeId";
                        int s = DBHelper.ExecuteNonQuery(sql, new SqlParameter("@CTypeId", CTypeId));
                        if (s != 0)
                        {
                            this.ShowSuccessTip("成功删除选中行数据！");
                            //MessageBox.Show("成功删除选中行数据！");
                        }
                    }
                    break;
                case DialogResult.No:
                    break;
            }
        }
    }
}
